define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var util = require('util');
	var grid = require('@base/highGrid');
	var countSelector = require('modules/countSelector');

	// 基础频道分类
	var BaseCategory = grid.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'M-HighGrid P-campaignList',
				'url': 'channelCategory/list',
				'updateUrl': 'campaign/setChannelCategoryPremium',
				'cols': [
				],
				'param': {
					order: "cost|-1"
				},
				'tab': pubjs.config('metricsGroup/channelCategory/tabs'),
				'default_metrics' : pubjs.config('defaultMetrics'),
				'hasAmount': true,
				'reqType': 'websocket',
				'gridName': 'channelCategory',
				'hasSearch': false,
				'hasFilter': false,
				'hasDateRange': true,
				'key': 'page_channel_id',
				'nameKey': 'page_channel_name',
				'subs': [
					'channelCategory2'
				],
				'subFilter': 'subFilter',
				'exportParam': 'channelCategory'
			});
			this.Super('init', arguments);
		},
		subFilter: function(subs, data){

		},
		renderName: function(i, val, data){
			return val || '-';
		},
		renderTopPrice: function(i, val, row, con){
			return this.formatCurrency(+row.set_price || 0);
		},
		renderNSpeculator: function(i, val, row, con){
			var value = 0;
			value = row.premium || 0;
			var data = {
				'premium': +value,
				'page_channel_name': row.page_channel_name || '',
				'page_channel_id': row.page_channel_id || '',
				'page_channel2_name': row.page_channel2_name || '',
				'page_channel2_id': row.page_channel2_id || ''
			};
			var iconMap = ['editCPM', 'editCPA', 'editCPC', 'editCPC', 'editCPM'];
			var icon = iconMap[row.WardenStatus] || 'editCOM';
			var dom = $([
				'<div class="uk-text-truncate">',
					'<i class="status status-'+icon+' mr5" style="vertical-align: -1px;"></i>'+ '<span>'+this.formatCurrency(+value || 0)+'</span>',
				'</div>'
				].join(''));
			this.uiBind(dom.find('i'), 'click', data, 'eventChangeTopPrice');
			return dom;
		},
		eventChangeTopPrice: function(ev, elm){
			var self = this;
			var data = self.$nSpeculatorData = ev.data;
			data.value_elm = $(elm).next('span');	// 值显示元素

			// 传入活动id
			var param = self.getParam();
			if(param && param.condition){
				var condition = JSON.parse(param.condition);
				var campaign_id = '';
				util.each(condition, function(item, idx){
					if(item && item.hasOwnProperty('campaign_id')){
						campaign_id = item['campaign_id'];
						return;
					}
				});
				if(campaign_id){
					data.campaign_id = campaign_id;
				}else{
					return false;
				}
			}

			var mod = self.get('nSpeculator');
			if(!mod){
				mod = self.create('nSpeculator', countSelector.base, {
					prefix: "￥",		//字段前缀
					suffix: LANG('元'),//字段后缀
					addCount: 0.5,
					value: data.premium
				});
			}

			mod.show({
				anchor: $(elm).offset(),
				data: data.premium
			});

			return false;
		},
		formatCurrency: function(val){
			val = util.round0(val, 2);
			return (LANG("￥"))+util.numberFormat(val);
		},
		onUpdateValue: function(ev){
			if(+ev.param < 0){
				// pubjs.alert(LANG('价格必须为一个正数'));
				// return false;
			}

			if(!util.isNumber(+ev.param)){
				pubjs.alert(LANG('价格信息错误'));
				return false;
			}
			this.save(+ev.param);

			return false;
		},
		save: function(premium){
			var self = this;
			var url = self.getConfig('updateUrl');

			var data = self.$nSpeculatorData;
			data.premium = +premium;

			var result = {
				'Stake': +premium,
				'Type': 5
			};
			var PageClass = [];

			// 如果是二级溢价
			if(data.page_channel2_id){
				PageClass.push({
					'name': data.page_channel2_name,
					'id': data.page_channel2_id
				});
			}else{
				// 如果是一级溢价
				PageClass.push({
					'name': data.page_channel_name,
					'id': data.page_channel_id
				});
			}
			result.PageClass = PageClass;

			pubjs.mc.send(url, {
				'campaign_id': data.campaign_id,
				'page_channel_data': result
			}, self.onSaveValue.bind(self));

			if(self.$.nSpeculator){
				self.$.nSpeculator.destroy();
			}
			return self;
		},
		onSaveValue: function(err, data){
			if(err){
				pubjs.alert(err.message);
			}
			pubjs.alert(LANG('修改成功'));

			var nsData = this.$nSpeculatorData;
			if(nsData){
				// 更新当前值
				nsData.value_elm.text(this.formatCurrency(+nsData.premium));
			}
			// 暂时不刷新
			// this.reload();
			nsData = {};
		}
	});

	// 汇总频道分类
	var Amount = BaseCategory.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'cols': [
					{name:"page_channel_id", text: LANG('一级频道分类ID'),align:'center', width: 100},
					{name:'page_channel_name',text: LANG('一级频道分类'),render: 'renderName',type:'index',width: 120},
					{name:"page_channel2_id", text: LANG('二级频道分类ID'),align:'center', width: 100},
					{name:'page_channel2_name',text: LANG('二级频道分类'),"render": "renderName",width: 120,align:'left'},
					{name:'NSpeculator', text: LANG('差异出价'), align:'right', render:'renderNSpeculator', sort:false, width: 80}
				],
				'param': {
					order: "cost|-1"
				},
				'gridName': 'amount_channelCategory',
				'subs': null
			});
			this.Super('init', arguments);
		}
	});
	exports.amount = Amount;


	// 一级频道分类
	var ChannelCategory = BaseCategory.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'cols': [
					{name:"page_channel_id", text: LANG('一级频道分类ID'),align:'center', width: 100},
					{name:'page_channel_name',text: LANG('一级频道分类'),render: 'renderName',type:'index',width: 120},
					{name:'set_price', text: LANG('出价'), align:'right', render:'renderTopPrice', sort:false, width: 60},
					{name:'NSpeculator', text: LANG('差异出价'), align:'right', render:'renderNSpeculator', sort:false, width: 80}
				],
				'param': {
					order: "cost|-1",
					isOne: true
				},
				'gridName': 'channelCategory',
				'key': 'page_channel_id',
				'nameKey': 'page_channel_name',
				'subs': [
					'channelCategory2'
				],
				'subFilter': 'subFilter',
				'hasAmountSubGrid': true,
				'amountSubgridName': LANG('汇总'),
				'amountSubs': ['amount_channelCategory']
			});
			this.Super('init', arguments);
		}
	});
	exports.base = ChannelCategory;


	// 二级频道分类
	var ChannelCategory2 = BaseCategory.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'cols': [
					{name:"page_channel2_id", text: LANG('二级频道分类ID'),align:'center', width: 100},
					{name:'page_channel2_name',text: LANG('二级频道分类'),"render": "renderName",width: 120,align:'left'},
					{name:'set_price', text: LANG('出价'), align:'right', render:'renderTopPrice', sort:false, width: 60},
					{name:'NSpeculator', text: LANG('差异出价'), align:'right', render:'renderNSpeculator', sort:false, width: 80}
				],
				'param': {
					order: "cost|-1",
					filter: true,
					limit: 1000
				},
				'gridName': 'channelCategory2',
				'key': 'page_channel2_id',
				'nameKey': 'page_channel2_name'
			});

			var cParam = config.get().param;
			var condition = cParam.condition;
			if(condition){
				condition = JSON.parse(condition);
				cParam.condition = JSON.stringify([condition[0]]);
				cParam.parentId = condition[1].channelCategory_id;
			}

			this.Super('init', arguments);
		}
	});
	exports.base2 = ChannelCategory2;

});